/**
    Copyright (c) 2010 ATTi 
    All rights reserved.
    Author: Michael Shu
    Date: 10/7/2010
*/

trigger OpportunityAASFieldUpdateTrigger on Opportunity (before insert, before update, after insert, after update,before delete, after delete) {
    Set<ID> oppAASRecordTypeID = AASFieldUpdateHelperClass.getOpportunityAASRecordTypeID();
    if(trigger.isBefore){
        List<Opportunity> opportunities = new List<Opportunity>(); // mshu : AAS field update
        List<Opportunity> opportunities_insert = new List<Opportunity>(); // mshu : AAS field update
        List<Opportunity> opportunities_update = new List<Opportunity>(); // mshu : AAS field update
        Set<Id> OpportunityIds = new Set<Id>();
        if(trigger.isInsert){
            for(Opportunity o : trigger.new){
                if(oppAASRecordTypeID.contains(o.RecordTypeId)){
                    opportunities.add(o);
                    opportunities_insert.add(o);
                }
            }
        }else if(trigger.isDelete){
            for(Opportunity o : trigger.old){
                if(oppAASRecordTypeID.contains(o.RecordTypeId) && o.Probability >= 90.00){
                    o.addError('You Cannot Delete Opportunities with probability 90% or higher');
                }
            }
        }else if(trigger.isUpdate){
             for(Opportunity o : trigger.new){
                Opportunity oldOpp = trigger.oldMap.get(o.Id);
                if(oldOpp.Probability != o.Probability && o.Probability == 100.00 && oppAASRecordTypeID.contains(o.RecordTypeId)){
                     OpportunityIds.add(o.id);
                }
                if(o.OwnerId != oldOpp.OwnerId && oppAASRecordTypeID.contains(o.RecordTypeId)){
                	opportunities.add(o);
                }
                if(oppAASRecordTypeID.contains(o.RecordTypeId)){
                    opportunities_update.add(o);
                }
            }
        }
        if(!OpportunityIds.isEmpty()){
            AASOpportunityCheck.ValidateFulfillmentCompleted(OpportunityIds ,trigger.newMap);
        }
        if(!opportunities.isEmpty()){
            AASFieldUpdateHelperClass.updateAASOpportunityfieldsInBeforeEvent(opportunities);
        }
        if(!opportunities_insert.isEmpty() || !opportunities_update.isEmpty()){
            AASFieldUpdateHelperClass.stampPOIndexAccountFieldsAndValidateInInsertAndUpdate(opportunities_insert,opportunities_update, trigger.oldMap);
        }
        if(!trigger.isDelete){
            AASFieldUpdateHelperClass.setAASOppfields(trigger.new);  
        }       
    }else if(trigger.isAfter){
        Set<ID> opportunityids_after_insert = new Set<ID>(); // mshu : AAS field update
        Set<ID> opportunityids_after_AccountChange = new Set<ID>(); // mshu : AAS field update
        if(trigger.isInsert){
            AASFieldUpdateHelperClass.setAASAgencyAccountIdsInDeleteOrInsert(trigger.new);
        }else if(trigger.isUpdate){
            AASFieldUpdateHelperClass.setAASAgencyAccountIdsInUpdate(trigger.new, trigger.oldMap);
        }else if(trigger.isDelete){
            AASFieldUpdateHelperClass.setAASAgencyAccountIdsInDeleteOrInsert(trigger.old);
        }
        AASFieldUpdateHelperClass.refresh_Agency_Acct_12_month_sold();
        
        if(trigger.isInsert){
            for(Opportunity o : trigger.new){
                if(oppAASRecordTypeID.contains(o.RecordTypeId)){
                    if(o.PO_Index__c == null || o.AAS_Agency__c == null || o.AAS_Master_Client__c == null){
                        opportunityids_after_insert.add(o.Id);
                    }
                }
            }
            if(!opportunityids_after_insert.isEmpty()){
                AASFieldUpdateHelperClass.stampPOIndexAccountFieldsInAfterEvent(opportunityids_after_insert);
            }
        }
        if(trigger.isUpdate){
            Opportunity OldOpp;            
            Set<Id> OppIds = new Set<Id>();            
            for(Opportunity o : trigger.new){
                OldOpp = trigger.oldMap.get(o.id);
                if(o.Probability < 90.00 && o.Order_Initiated__c && OldOpp.Probability >= 90.00 && oppAASRecordTypeID.contains(o.RecordTypeId)){
                    OppIds.add(o.id);
                }else if(oldOpp.NPO_Id__c != o.NPO_Id__c && oppAASRecordTypeID.contains(o.RecordTypeId)){
                    opportunityids_after_AccountChange.add(o.Id);
                }
            }
            if(!OppIds.isEmpty()){
                AASOpportunityCheck.DeleteExistingOrders(OppIds);
            }
            if(!opportunityids_after_AccountChange.isEmpty()){
                AASFieldUpdateHelperClass.updateNAOIDofFulfillmentOrders(opportunityids_after_AccountChange);
            }
        }
    }
}